package controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import dto.CompanyDTO;
import dto.ListProductPageDTO;
import dto.ProductDTO;

import service.ProductManageService;

@Controller
public class ProductController {
	private ProductManageService service;
	
	public ProductController(ProductManageService service) {
		this.service = service;
	}
	@RequestMapping("/mainPage.do")
	public ModelAndView mainPage(){
		ModelAndView mv = null;
		try {
			HashMap map = new HashMap();
			map.put("byReadCnt", service.searchByReadCnt());
			map.put("byLately", service.searchByLately());
			map.put("byRate", service.searchByRate());
			mv = new ModelAndView("mainPage_tiles.do", "list", map);
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do", "error_message", e.getMessage());
		}
		return mv;
	}
	
	@RequestMapping("/todayCoupon.do")
	public ModelAndView todayCoupon(){
		ModelAndView mv = null;
		try{
		ArrayList<String> locationList = service.searchAllLocation();
		ArrayList<String> categoryList = service.serchAllCategory();
		
		HashMap map = new HashMap();
		map.put("location", locationList);
		map.put("category", categoryList);
		
		mv = new ModelAndView("todayCoupon_tiles.do", "list", map);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do", "error_message", e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/todayCoupon_search.do")
	public ModelAndView todayCoupon_search(String[] byLocation, String[] byCategory, HttpServletRequest request){
		ModelAndView mv = null;
		ListProductPageDTO pagingPdto = null;
		
		int page = 1;
		try {
			if(request.getParameter("page")!=null){
				page = Integer.parseInt(request.getParameter("page"));
			}
			else{
				page=1;
			}
			pagingPdto = service.getProductListByPage(page, byLocation, byCategory);
			mv = new ModelAndView("jsonView","paging", pagingPdto);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do", "error_message", e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/product_detail.do")
	public ModelAndView productDetail(String productId ){
		ModelAndView mv = null;
		ProductDTO pdto = null;
		try {
			int id = Integer.parseInt(productId);
			pdto = service.searchByProductId(id);
			mv = new ModelAndView("productDetail_tiles.do", "pdto", pdto);
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do", "error_message", e.getMessage());
		} catch (NumberFormatException ne ){
			mv = new ModelAndView("error.do", "error_message", "상품이 없습니다.. 다시 메인으로..");
		}
		
		return mv;
	}
	@RequestMapping("/product_register.do")
	public ModelAndView productRegister(ProductDTO pdto, HttpSession session){
		ModelAndView mv = null;
		if((session.getAttribute("companyDTO"))==null){
			mv = new ModelAndView("error.do", "error_message", "로그인부터 하십시오");
			return mv;
		}
		try {
			//companyId 등록
			pdto.setCompanyId(((CompanyDTO) session.getAttribute("companyDTO")).getId());
			service.registerProduct(pdto);
			mv = new ModelAndView("productDetail_tiles.do", "pdto", pdto);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do", "error_message", e);
		}
		return mv;
	}
	@RequestMapping("/register_form.do")
	public ModelAndView registerForm(){
		ModelAndView mv = null;
		try {
			ArrayList<String> locationList = service.searchAllLocation();
			ArrayList<String> categoryList = service.serchAllCategory();
			
			HashMap map = new HashMap();
			map.put("location", locationList);
			map.put("category", categoryList);
			
			mv = new ModelAndView("registerProduct_tiles.do", "list", map);
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do", "error_message", e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/modify_form.do")
	public ModelAndView modifyForm(int productId, HttpSession session){
		ModelAndView mv = null;
		
		if((session.getAttribute("companyDTO"))==null){
			mv = new ModelAndView("error.do", "error_message", "로그인부터 하십시오");
			return mv;
		}
		try {
			ArrayList<String> locationList = service.searchAllLocation();
			ArrayList<String> categoryList = service.serchAllCategory();
			ProductDTO pdto = service.searchByProductId(productId);
			HashMap map = new HashMap();
			map.put("location", locationList);
			map.put("category", categoryList);
			map.put("pdto", pdto);
			mv = new ModelAndView("modify_form_tiles.do", "list", map);
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do", "error_message", e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/product_modify.do")
	public ModelAndView modifyProduct(ProductDTO pdto){
		ModelAndView mv = null;
		try {
			service.modifyProduct(pdto);
			mv = new ModelAndView("productDetail_tiles.do", "pdto", pdto);
		} catch (Exception e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do", "error_message", e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/deleteProduct.do")
	public ModelAndView deleteProduct(int productId, HttpSession session){
		ModelAndView mv = null;
		if((session.getAttribute("companyDTO"))==null){
			mv = new ModelAndView("/jsp/error.jsp", "error_message", "로그인부터 하십시오");
			return mv;
		}
		try {
			service.deleteProduct(productId);
			mv = new ModelAndView("delete_success_tiles.do");
		} catch (SQLException e) {
			e.printStackTrace();
			mv = new ModelAndView("error.do", "error_message", e.getMessage());
		}
		return mv;
	}
	@RequestMapping("/deleteProductImage.do")
	public ModelAndView deleteFile(HttpSession session, int productId, String productImageFilename){
		ModelAndView mv = null;
		if(session.getAttribute("companyDTO")==null){
			mv = new ModelAndView("error.do", "error_message", "로그인부터 하십시오..");
			return mv;
		}
		try{
			ProductDTO pdto = service.searchByProductId(productId);
			pdto.setProductImageFilename(productImageFilename);
			service.deleteFile(pdto);//파일삭제
			HashMap map = new HashMap();
			map.put("pdto", pdto);
			mv = new ModelAndView("modify_form_tiles.do", "list", map);
		}catch(Exception e){
			e.printStackTrace();
			mv = new ModelAndView("error.do", "error_message", e.getMessage());
		}
		return mv;
	}
}
